package com.laowang.project.system.service.impl;

import java.beans.Transient;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import com.laowang.common.utils.DateUtils;
import com.laowang.framework.redis.RedisCache;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.project.system.domain.*;
import com.laowang.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.laowang.project.system.mapper.MyLifeCircleLikeMapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 车友圈点赞Service业务层处理
 *
 * @author laowang
 * @date 2023-04-25
 */
@Service
public class MyLifeCircleLikeServiceImpl implements MyLifeCircleLikeService {
    @Autowired
    private MyLifeCircleLikeMapper myLifeCircleLikeMapper;

    @Autowired
    private MyLifeCircleService myLifeCircleService;

    @Autowired
    private MyCyhSocreService myCyhSocreService;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private UserPointLogService userPointLogService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询车友圈点赞
     *
     * @param id 车友圈点赞ID
     * @return 车友圈点赞
     */
    @Override
    public MyLifeCircleLike selectMyLifeCircleLikeById(Integer id) {
        return myLifeCircleLikeMapper.selectMyLifeCircleLikeById(id);
    }

    /**
     * 查询车友圈点赞列表
     *
     * @param myLifeCircleLike 车友圈点赞
     * @return 车友圈点赞
     */
    @Override
    public List<MyLifeCircleLike> selectMyLifeCircleLikeList(MyLifeCircleLike myLifeCircleLike) {
        return myLifeCircleLikeMapper.selectMyLifeCircleLikeList(myLifeCircleLike);
    }

    /**
     * 新增车友圈点赞
     *
     * @param myLifeCircleLike 车友圈点赞
     * @return 结果
     */
    @Override
    public int insertMyLifeCircleLike(MyLifeCircleLike myLifeCircleLike) {
        return myLifeCircleLikeMapper.insertMyLifeCircleLike(myLifeCircleLike);
    }

    /**
     * 修改车友圈点赞
     *
     * @param myLifeCircleLike 车友圈点赞
     * @return 结果
     */
    @Override
    public int updateMyLifeCircleLike(MyLifeCircleLike myLifeCircleLike) {
        return myLifeCircleLikeMapper.updateMyLifeCircleLike(myLifeCircleLike);
    }

    /**
     * 批量删除车友圈点赞
     *
     * @param ids 需要删除的车友圈点赞ID
     * @return 结果
     */
    @Override
    public int deleteMyLifeCircleLikeByIds(Integer[] ids) {
        return myLifeCircleLikeMapper.deleteMyLifeCircleLikeByIds(ids);
    }

    /**
     * 删除车友圈点赞信息
     *
     * @param id 车友圈点赞ID
     * @return 结果
     */
    @Override
    public int deleteMyLifeCircleLikeById(Integer id) {
        return myLifeCircleLikeMapper.deleteMyLifeCircleLikeById(id);
    }

    @Transactional
    @Override
    public Integer addLike(MyLifeCircleLike myLifeCircleLike) {
        myLifeCircleLikeMapper.insertMyLifeCircleLike(myLifeCircleLike);
        // 点赞人
        Integer userId = myLifeCircleLike.getUserId();
        Integer i =null;
        MyCyhSocre myCyhSocre = myCyhSocreService.selectMyCyhSocreByType(2);
        synchronized (this){
            MyLifeCircle myLifeCircle = myLifeCircleService.selectMyLifeCircleById(myLifeCircleLike.getLifeCircleId());
            Integer likeNum = myLifeCircle.getLikeNum();
            likeNum++;
            myLifeCircle.setLikeNum(likeNum);
            myLifeCircleService.updateMyLifeCircle(myLifeCircle);

            if (myCyhSocre !=null  ){
                Integer score = myCyhSocre.getScore();
                Integer upperLimit = myCyhSocre.getUpperLimit();
                //是否已经满次数了
                //没有副牌每天看一次
                CacheObj cacheObj = redisCache.getCacheObject("like:" + userId);
                if (Objects.isNull(cacheObj)){
                    CacheObj cacheObj1 = new CacheObj();
                    cacheObj1.setDate(new Date());
                    cacheObj1.setNum(1);
                    redisCache.setCacheObject("like:"+userId,cacheObj1,2, TimeUnit.DAYS);

                }else {
                    Integer num = cacheObj.getNum();
                    Date date = cacheObj.getDate();
                    Date date1 = new Date();
                    if (DateUtils.isSameDay(date1,date)){
                        if (num.equals(upperLimit)){
                            return 0;
                        }
                        num++;
                        cacheObj.setNum(num);
                        redisCache.setCacheObject("like:"+userId,cacheObj,2, TimeUnit.DAYS);
                    }else {

                        CacheObj cacheObj1 = new CacheObj();
                        cacheObj1.setDate(new Date());
                        cacheObj1.setNum(1);
                        redisCache.setCacheObject("like:"+userId,cacheObj1,2, TimeUnit.DAYS);

                    }




                }

                //加积分
                WxUser wxUser = wxUserService.selectWxUserById(userId);
                Integer points = wxUser.getPoints();
                Integer newPoint = points + score;
                wxUser.setPoints(newPoint);
                wxUserService.updateWxUser(wxUser);
                //积分日志
                UserPointLog userPointLog = new UserPointLog();
                userPointLog.setRemark("点赞加积分");
                userPointLog.setOldPoint(points);
                userPointLog.setChangePoint(score);
                userPointLog.setNewPoint(newPoint);
                userPointLog.setUserId(userId);
                userPointLog.setType(2);
                userPointLog.setCreated(new Date());
                userPointLogService.insertUserPointLog(userPointLog);
                i = score;
            }
        }
        return i;
    }
}
